package com.walter.epay.util;

import cn.hutool.core.util.StrUtil;
import com.walter.epay.exception.EpaySignException;
import com.walter.epay.param.EpaySignParam;

import java.security.NoSuchAlgorithmException;

/**
 * sign值获取工具类
 *
 * @author wuchenxi
 * @date 2022-12-31 14:09:29
 */
public class EpaySignUtil {

    /**
     * 获取sign字符串
     *
     * @param signParam
     * @throws NoSuchAlgorithmException 加密异常
     * @return
     */
    public static String getSign(EpaySignParam signParam) throws NoSuchAlgorithmException {
        checkParam(signParam);

        return EpayMD5Util.md5(signParam.getGenerateSignStr());
    }

    private static void checkParam(EpaySignParam signParam) {
        if (signParam == null) {
            throw new EpaySignException("SignParam 参数不能为空");
        }
        if (signParam.getPrice() == null) {
            throw new EpaySignException("SignParam price 参数不能为空");
        }
        if (signParam.getType() == null) {
            throw new EpaySignException("SignParam type 参数不能为空");
        }
        if (StrUtil.isBlank(signParam.getSecret())) {
            throw new EpaySignException("SignParam secret 参数不能为空");
        }
    }
}
